package de.tum.in.eist.poll.client;

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import com.google.gwt.user.client.rpc.AsyncCallback;

import de.tum.in.eist.poll.pub.shared.Feedback;
import de.tum.in.eist.poll.pub.shared.Lecture;

/**
 * This object works as container for statistical data
 */
public class Stats {
	private int[] opinions = { 0, 0, 0, 0, 0 };
	private LinkedList<String> likes = new LinkedList<String>();
	private LinkedList<String> dislikes = new LinkedList<String>();
	private Lecture lecture = null;
	private List<Feedback> feedbackList = null;
	private Chart chart;

	/**
	 * Initializes a Stats-object
	 * 
	 * @param lecture is the lecture the statistical data are about
	 * @param chart is the chart, where this data are used
	 * @param admin is an integer which proves that the user is an admin
	 * @param view is an object which contains the AdminView
	 */
	public Stats(Lecture lecture, Chart chart, int admin, AdminStatisticView view) {
		this.lecture = lecture;
		this.chart = chart;
		getData(admin, view);
	}
	
	
	/**
	 * This function has to download the feedbackList and add it to the Stats-object 
	 * 
	 * @param admin is an integer which proves that the user is an admin
	 * @param view is an object which contains the AdminView
	 */
	public void getData(final int admin, final AdminStatisticView view){
		AsyncCallback<List<Feedback>> callback = new AsyncCallback<List<Feedback>>() {

			@Override
			public void onSuccess(List<Feedback> result) {
				feedbackList = result;
				analyze();
				chart.draw();
				if (admin == 1)
					view.makeLikes();
			}

			@Override
			public void onFailure(Throwable caught) {
				opinions = null;

			}
		};
		EIST_POLL.service.getFeedback(lecture, callback);
	}
	
	/**
	 * This function uses the data of the feedbackList to add positive and negative 
	 * feedback to likes and dislike lists.
	 */
	private void analyze(){
		if(feedbackList == null){
			opinions = null;
			return;
		}
		ListIterator<Feedback> iterator = feedbackList.listIterator();
		while (true) {
			try {
				Feedback feedback = (Feedback) iterator.next();
				this.addOpinion(feedback.getOpinion());
				if (feedback.getLike() != "")
					this.addLike(feedback.getLike());
				if (feedback.getDislike() != "")
					this.addDislike(feedback.getDislike());
			} catch (Exception e) {
				break;
			}
		}
	}
	
	/**
	 * Adds one opinion to the opinion-array
	 * 
	 * @param opinion is an integer, which reflects the opinion of one user about one lecture
	 */
	public void addOpinion(int opinion) {
		if (opinion >= 0 && opinion <= 5)
			opinions[opinion]++;
	}

	/**
	 * Adds one positive feedback to the likes-list
	 * 
	 * @param like is a positive feedback
	 */
	public void addLike(String like) {
		likes.add(like);
	}

	/**
	 * Adds one negative feedback to the dislikes-list
	 * 
	 * @param dislike is a negative feedback
	 */
	public void addDislike(String dislike) {
		dislikes.add(dislike);
	}

	/**
	 * @return A array of opinions.
	 */
	public int[] getOpinions() {
		return opinions;
	}
	
	/**
	 * @return A List of all positive feedbacks
	 */
	public LinkedList<String> getLike() {
		return likes;
	}

	/**
	 * @return A List of all negative feedbacks
	 */
	public LinkedList<String> getDislike() {
		return dislikes;
	}
	
	/**
	 * @return The lecture of this Stats-object
	 */
	public Lecture getLecture() {
		return lecture;
	}
}
